#define VIDEO_INDEX_PORT			0x3D4
#define VIDEO_DATA_PORT				0x3D5
#define VIDEO_MEM_BASE_ADDRESS		0xB800
#define VIDEO_MASK					0x3FFF

int origin;
int row, column;
int color;

void writeVideoData(int reg, int value)
{
	mem_lock();	
	write_port(VIDEO_INDEX_PORT, reg);
	write_port(VIDEO_DATA_PORT, value);	
	mem_unlock();
}

void writeVideoWord(int reg, int value)
{
	writeVideoData(reg, (value >> 8) & 0xFF);
	writeVideoData(reg + 1, value & 0xFF);
}

void initializeDisplayDriver()
{
	origin = 0;
	color = 0x0A;
	row = 0;
	column = 0;
	
	writeVideoWord(10, 15);
	writeVideoWord(12, 0);
	writeVideoWord(14, 0);
}

void dputc(char c)
{
	int position, word, offset;
	
	position = 2 * ((row * 80) + column);
	offset = (origin + position) & VIDEO_MASK;
	
	word = color;
	word = (word << 8) + c;
	
	putWord(word, VIDEO_MEM_BASE_ADDRESS, offset);
	
	++column;
	
	position = 2 * ((row * 80) + column);
	offset = (origin + position) & VIDEO_MASK;
	writeVideoWord(10, offset >> 1);
}
